/**
 * Global variables and functions
 */
var Upbeat = (function($, window, undefined) {
    var privateVar = 1;

    function innitDropdownList(tagDropdown, idInptHidden) {
        $(tagDropdown.attr('data-target')).find('ul').children('li').on('click', function(e) {
            e.preventDefault();
            $(tagDropdown).text($(this).find('a').text());
        });
    };

    return {
        publicVar: 1,
        publicObj: {
            var1: 1,
            var2: 2
        },
        innitDropdownList: innitDropdownList
    };
})(jQuery, window);

/**
 * Website start here
 */
jQuery(document).ready(function($) {
    /*Homepage carousel*/
    var _hpCarousel = $('#dg-container');
    if (_hpCarousel.length > 0) {
        _hpCarousel.gallery({
            autoplay: true
        });
    }
    /*All carousel*/
    $('#carousel-1').carousel();
    $('#carousel-2').carousel();
    $('#carousel-3').carousel();
    $('#myCarousel').carousel({
        interval: 5000
    });

    $('#myCarousel-thumbs').carousel({
        interval: 5000
    });

    $('#myCarousel').on('slid.bs.carousel', function() {
        $('.list-group-item').find('a').removeClass('active');
        $('.list-group-item').eq($(this).find('.item.active').index()).find('a').addClass('active');
    });

    $('#myCarousel-control .list-group').children('li').each(function(idx, elm) {
        $(elm).on('click', function() {
            $('.list-group-item').find('a').removeClass('active');
            $('.list-group-item').eq(idx).find('a').addClass('active');
            $('#myCarousel-thumbs').carousel(idx);
        });
    });

    $('#myCarousel, #myCarousel-thumbs').on('mouseover', function() {
        $('#myCarousel').carousel('pause');
        $('#myCarousel-thumbs').carousel('pause');
    });

    $('#myCarousel, #myCarousel-thumbs').on('mouseleave', function() {
        $('#myCarousel').carousel('cycle');
        $('#myCarousel-thumbs').carousel('cycle');
    });

    $('#myCarousel-1').carousel();
    $('#myCarousel-3').carousel();
    $('#myCarousel-2').carousel();
    $('#myCarousel-4').carousel({
        interval: 10000
    });

    $('.tags-well').each(function(idx, item) {
        $(item).on('click', function() {
            var $parent = $(this).parent();
            $('.tags-well').parent().removeClass('active');
            if ($('.' + $(this).attr('data-rel')).hasClass('active')) {
                $('.' + $(this).attr('data-rel')).slideUp();
                $('.' + $(this).attr('data-rel')).removeClass('active');
            } else {
                $('.wrap-well').not($('.' + $(this).attr('data-rel'))).slideUp();
                $('.wrap-well').removeClass('active');
                $('.' + $(this).attr('data-rel')).slideDown();
                $('.' + $(this).attr('data-rel')).addClass('active');
                $parent.addClass('active');
            }
        })
    });

    /*All dropdown*/
    $('.dropdown-toggle').dropdown();

    /*All datepicker*/
    $('#datepicker').datepicker({
        onChangeMonthYear: function(y, m, d) {
            setTimeout(function() {
                $('.ui-datepicker-calendar').addClass('in-month-' + m);
            }, 100);
        }
    });

    var d = new Date();
    var monthBegin = d.getMonth() + 1;

    $('.ui-datepicker-calendar').addClass('in-month-' + monthBegin);

    $('.options').popover({
        placement: 'right',
        html: true,
    });

    /*$('#payment-modal').modal('show');
    $('#login-modal').modal('show');*/

    /**Animate Search Block**/
    $('.list-1 .btn-search').not('.add-on').on('click', function() {
        var _this = $(this);
        _this.css('display', 'none');
        _this.parents('.list-1').css('display', 'none');
        $('.form-search').removeClass('hidden');
    });

    /**Init JS Menu Mobile**/
    var startX, curX, startY, curY; // Variables
    var newXScroll, newYScroll, genXScroll; // More Variables!

    // Change the height of the sidebar, as well as a few things to do with the main content area, so the user
    // can actually scroll in the content area.
    function sideBarHeight() {

        var docHeight = $(document).height();
        var winHeight = $(window).height();

        $('.slide-in').height(winHeight);
        $('#container').height(winHeight);
        // $('#sub-container').height($('#sub-container').height());
    }

    // Run the function on resize and when the page loads!
    $(window).resize(function() {
        sideBarHeight();
    });
    sideBarHeight();

    var outIn = 'in';

    Hammer(document.getElementById('container')).on('swiperight', function(e) {
        $('.slide-in').toggleClass('on');
        $('#container').toggleClass('on');
        outIn = 'out';

    });

    Hammer(document.getElementById('container')).on('swipeleft', function(e) {
        $('.slide-in').toggleClass('on');
        $('#container').toggleClass('on');
        outIn = 'in';
    });

    function runAnimation() {

        if (outIn == 'out') {

            $('.slide-in').toggleClass('on');
            $('#container').toggleClass('on');
            outIn = 'in';

        } else if (outIn == 'in') {

            $('.slide-in').toggleClass('on');
            $('#container').toggleClass('on');
            outIn = 'out';

        }

    }

    $('.btn-user').click(function() {
        $('.slide-in').toggleClass('on');
        $('#container').toggleClass('on');
    });

    $('.more-details > a').each( function() {
        
        $(this).on('click', function () {
            var _this = $( this );

            setTimeout(function() {
                if( $(_this.attr('href')).hasClass('in') ) {
                    _this.text('Less Details');
                }else {
                    _this.text('More Details');
                }
            },1000);
        });
    });
});